package gebruikersinterface;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import domein.DomeinController;

@SuppressWarnings("serial")
public class Consumpties extends Hoofdscherm {
	
	final int afstandX = 30;
	final int afstandY = 10;
	final int x = 150;
	final int y = 50;
	
	public Consumpties(Scherm scherm){
		super(scherm);
		this.setLayout(null);	
		this.init();
	}
	
	public void refresh(){
		
	}
	
	public void init(){
		this.removeAll();
		
		String[][] consumpties = DomeinController.getInstance().getConsumpties();
		
		int width = 724;
		//int height = 768;
		
		int n = consumpties.length;
		int afstandHor = 20;
		int afstandVer = 20;
		for(int i=0;i<n;i++){
			JButton button = new JButton(String.format("<html><body style=\"text-align:center\";>%s<br>%s</body></html>",consumpties[i][1],consumpties[i][2]));
			button.setBounds(afstandHor, afstandVer, x, y);
			final int id = Integer.parseInt(consumpties[i][0]);
			button.addActionListener(new ActionListener(){
				@SuppressWarnings("static-access")
				public void actionPerformed(ActionEvent event){
					try {
						DomeinController.getInstance().addRekeningLijn(id);
						DomeinController.getInstance().updateVoorraadExtra(id);
						Thread.currentThread().sleep(500);
						geklikt();
					} catch (Exception e) {
					}
				}
			});
			this.add(button);
			afstandHor = afstandHor + x + afstandX;
			if (afstandHor > width){
				afstandVer = afstandVer + y +afstandY;
				afstandHor = 20;
			}	
		}
		
		JButton button = new JButton("terug");
			button.setBounds(afstandHor,afstandVer, x, y);
			button.addActionListener( new ActionListener(){
				public void actionPerformed(ActionEvent e){
					scherm.setScherm(new Overzicht(scherm));
				}});
			this.add(button);
		
		this.repaint();
	}
	
	public void geklikt(){
		super.scherm.refresh();
	}
}